# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: https://docs.scrapy.org/en/latest/topics/item-pipeline.html


# useful for handling different item types with a single interface
from itemadapter import ItemAdapter
import pymysql

class JdphonePipeline:
    def open_spider(self,spider):
        self.db = pymysql.connect('localhost','root','1111','mydb')
        self.cursor = self.db.cursor()

    def process_item(self, item, spider):
        sql = '''insert into jdphone(pid,title,brand,time,cpu,memory,storage,screen,name,name_en,price,GoodCountStr,GeneralCountStr,PoorCountStr) values("%s","%s","%s","%s","%s","%s",
        "%s","%s","%s","%s","%s","%s","%s","%s")'''%(item['pid'],item['title'],item['brand'],
                                                     item['time'],item['cpu'],item['memory'],
                                                     item['storage'],item['screen'],item['name'],
                                                     item['name_en'],item['price'],item['GoodCountStr'],
                                                     item['GeneralCountStr'],item['PoorCountStr'],)
        # self.cursor.execute(sql)
        # self.db.commit()
        return item

    def close_spider(self,spider):
        self.cursor.close()
        self.db.close()